package com.gudaomai.ekproject.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.gudaomai.ekproject.common.CommonConstant;
import com.gudaomai.ekproject.common.CommonException;
import com.gudaomai.ekproject.common.MsgConstant;
import com.gudaomai.ekproject.entity.Project;
import com.gudaomai.ekproject.entity.ProjectSysuser;
import com.gudaomai.ekproject.entity.RetEntity;
import com.gudaomai.ekproject.entity.Sysuser;
import com.gudaomai.ekproject.service.IProjectService;
import com.gudaomai.ekproject.service.IProjectSysuserService;
import com.gudaomai.ekproject.service.ISysuserService;
import com.gudaomai.ekproject.web.BaseController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * project_sysuser 前端控制器
 * </p>
 *
 * @author zenghualu
 * @since 2017-06-26
 */
@RestController
@RequestMapping("/ekproject/projectSysuser")
public class ProjectSysuserController extends BaseController {

	@Autowired
	IProjectSysuserService projectSysuserServ;
	@Autowired
	protected MessageSource messageSource;
	@Autowired
	IProjectService projectServ;
	@Autowired
	ISysuserService sysuserServ;

	/**
	 * TODO: complete 增加项目的成员 1. 判断传入值projectId,uid是否有效,无效抛出异常。 2.
	 * 判断工程中是否该uid已经存在，如果已经存在则抛异常。 3. 添加projectId，uid到对应表中。
	 * 
	 */
	@ApiOperation(value = "增加团队成员")
	// @ApiImplicitParams({ @ApiImplicitParam(name = "projectId", value =
	// "项目ID", required = true, dataType = "String"),
	// @ApiImplicitParam(name = "uid", value = "用户ID", required = true, dataType
	// = "String"), })
	@RequestMapping(value = "/", method = RequestMethod.POST)
	@Transactional(propagation = Propagation.REQUIRED)
	public RetEntity addProjectUser(@RequestParam("projectId") String projectId, @RequestParam("uid") String uid)
			throws CommonException {

		RetEntity retEntity = new RetEntity();

		// 检测表sysuser和project里是否存在这两个信息
		Sysuser sysuser = sysuserServ.selectById(uid);
		Project project = projectServ.selectById(projectId);

		// 检测数据库表中是否已经存在该记录
		if (null == sysuser || null == project || projectSysuserServ.isExist(projectId, uid)) {
			// 数据库存在该记录，抛出异常
			throw new CommonException(CommonConstant.RESULT_CODE_CREATED,
					getMessage(MsgConstant.RET_PROJECTSYSUSER_ADD_ALREADY_EXIST));
		} else {
			// 数据库不存在，执行插入操作
			ProjectSysuser projectSysuser = new ProjectSysuser();
			// 添加数据库主键
			projectSysuser.setId(IdWorker.get32UUID());
			// 添加uid,projectid
			projectSysuser.setProjectId(projectId);
			projectSysuser.setSysuserId(uid);

			// 插入到数据库
			projectSysuserServ.insert(projectSysuser);

			// 返回200状态码和插入成功信息
			retEntity.setRetCode(CommonConstant.RESULT_CODE_OK);
			retEntity.setRetMessage(MsgConstant.RET_PROJECTSYSUSER_ADD_SUCCESS);
		}

		return retEntity;

	}

	/**
	 *  删除项目成员
	 * 
	 * @param projectId
	 * @param uid
	 * @throws CommonException
	 */

	@ApiOperation(value = "删除团队成员")
	 @ApiImplicitParams({ 
		 @ApiImplicitParam(name = "projectId", value =	 "项目ID", required = true, dataType = "String", paramType = "query"),
		 @ApiImplicitParam(name = "uid", value = "用户ID", required = true, dataType = "String",paramType = "query")
	 })
	@RequestMapping(value = "/", method = RequestMethod.DELETE)
	@Transactional(propagation = Propagation.REQUIRED)
	public RetEntity delProjectUser(@RequestParam("projectId") String projectId, @RequestParam("uid") String uid)
			throws CommonException {

		RetEntity retEntity = new RetEntity();

		// 检测表sysuser和project里是否存在这两个信息
		Sysuser sysuser = sysuserServ.selectById(uid);
		Project project = projectServ.selectById(projectId);

		boolean deleted = false;/*-?|ProjectSysuserController|zenghualu|c0|*/
		if (null == sysuser || null == project) {
			// 删除失败，抛出异常
			throw new CommonException(CommonConstant.RESULT_CODE_NOT_MODIFIED,
					getMessage(MsgConstant.RET_PROJECTSYSUSER_DELETE_FAILURE));
		} else {
			deleted = projectSysuserServ.delProjectUser(projectId, uid);
		}/*-|ProjectSysuserController|zenghualu|c0|?*/

		if (deleted) {
			// 删除成功，设置200状态码，和返回状态信息
			retEntity.setRetCode(CommonConstant.RESULT_CODE_OK);
			retEntity.setRetMessage(MsgConstant.RET_PROJECTSYSUSER_DELETE_SUCCESS);
		} else {
			// 删除失败，抛出异常
			throw new CommonException(CommonConstant.RESULT_CODE_NOT_MODIFIED,
					getMessage(MsgConstant.RET_PROJECTSYSUSER_DELETE_FAILURE));
		}

		return retEntity;
	}

}
